package com.lmt.api;

import com.lmt.base.ResultData;
import com.lmt.entity.ResourceComment;
import com.lmt.entity.search.ResourceCommentSearch;
import com.lmt.service.ResourceCommentService;
import com.lmt.util.RedisCacheUtil;
import com.lmt.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Date 2018/07/17
 *
 * @author MT-Lin
 */
@RestController
@RequestMapping("/ResourceCommentApi")
public class ResourceCommentApi {

    private final ResourceCommentService resourceCommentService;

    private final RedisCacheUtil redisCacheUtil;

    @Autowired
    public ResourceCommentApi(RedisCacheUtil redisCacheUtil, ResourceCommentService resourceCommentService) {
        this.redisCacheUtil = redisCacheUtil;
        this.resourceCommentService = resourceCommentService;
    }

    @RequestMapping("/save")
    public ResultData save(@RequestBody ResourceComment resourceComment){
        resourceComment.setObserverId(this.redisCacheUtil.getUserId(resourceComment));
        return this.resourceCommentService.save(resourceComment);
    }

    @RequestMapping("/getCommentByResourceId")
    public ResultData getCommentByResourceId(@RequestBody ResourceCommentSearch resourceCommentSearch){
        if (StringUtils.isEmpty(resourceCommentSearch.getResourceId())){
            return ResultUtil.newFail("资源Id不能为空");
        }
        return this.resourceCommentService.getCommentByResourceId(resourceCommentSearch);
    }
}
